//
//  HHRecommedController.m
//  gitosc01
//
//  Created by hjl on 16/3/20.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHProjectController.h"
#import "HHProjectCell.h"
#import <AFNetworking.h>
#import "HHProject.h"
#import "HHUser.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import "HHInfoViewController.h"


@interface HHProjectController ()

/** 项目模型数组 */
@property (strong, nonatomic) NSMutableArray *projects;
/** 页码 */
@property (nonatomic, assign) NSInteger page;

@end


static NSString * const HHProjectCellId = @"project";

@implementation HHProjectController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self setupRefresh];
    
    
}

- (void)setupRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewProjects)];
    
    [self.tableView.mj_header beginRefreshing];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreProjects)];
}

- (void)loadNewProjects{
    
    // 结束上拉
    [self.tableView.mj_footer endRefreshing];
    
    NSString *str = nil;
    if(self.type == HHProjectTypeRecommend) {
        str = @"featured?";
    } else if(self.type == HHProjectTypeHot){
        str = @"popular?";
    } else if(self.type == HHProjectTypeUpdata){
        str = @"latest?";
    }
    NSString *urlStr = [NSString stringWithFormat:@"https://git.oschina.net/api/v3/projects/%@", str];
    
    [[AFHTTPSessionManager manager] GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.projects = [HHProject mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        
        self.page = 1;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreProjects{
    
    // 结束下拉
    [self.tableView.mj_header endRefreshing];
    _page++;
    
    NSString *str = nil;
    if(self.type == HHProjectTypeRecommend) {
        str = @"featured?";
    } else if(self.type == HHProjectTypeHot){
        str = @"popular?";
    } else if(self.type == HHProjectTypeUpdata){
        str = @"latest?";
    }
    NSString *urlStr = [NSString stringWithFormat:@"https://git.oschina.net/api/v3/projects/%@", str];
    
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"page"] = @(_page);
    
    [[AFHTTPSessionManager manager] GET:urlStr parameters:parame progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *projects = [HHProject mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.projects addObjectsFromArray:projects];
        
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.tableView.mj_footer endRefreshing];
    }];
}


- (void)setupTableView{
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HHProjectCell class]) bundle:nil] forCellReuseIdentifier:HHProjectCellId];
    
    self.tableView.contentInset = UIEdgeInsetsMake(100, 0, 44, 0);
    
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    self.tableView.mj_footer.hidden = (self.projects.count == 0);
    
    return self.projects.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HHProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:HHProjectCellId forIndexPath:indexPath];
    
    HHProject *project = self.projects[indexPath.row];
    
    cell.project = project;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HHProject *project = self.projects[indexPath.row];
    return project.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HHProject * project = self.projects[indexPath.row];
    
    HHInfoViewController *infoVc = [[HHInfoViewController alloc] init];
    
    infoVc.project = project;
    
    [self.navigationController pushViewController:infoVc animated:YES];
}

@end
